package com.sky.myapplication.kotlin.kotlinDialog

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sky.myapplication.R

class BusinessSelectDialog(context: Context, list: List<DistrictBean>): DialogFragment(){

    private lateinit var mInflater: LayoutInflater
    private lateinit var rv_business_list: RecyclerView
    private val mContext: Context = context
    private val list: List<DistrictBean> = list

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mInflater = inflater
        val rootView = inflater.inflate(R.layout.dialog_business_select, null)
        findView(rootView)
        initView()
        return rootView
    }

    fun findView (rootView: View) {
        rv_business_list = rootView.findViewById(R.id.rv_business_list)
    }

    fun initView(){
        rv_business_list.layoutManager = LinearLayoutManager(mContext)
        val dividerItemDecoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)

        val drawable: Drawable? = ContextCompat.getDrawable(mContext, R.drawable.share_diverder)
        if (drawable != null) {
            dividerItemDecoration.setDrawable(drawable)
        }

        rv_business_list.addItemDecoration(dividerItemDecoration)
        rv_business_list.adapter = BusinessAdapter(list)
    }


    override fun onStart() {
        val params: WindowManager.LayoutParams = dialog!!.window!!.attributes
        params.width = ViewGroup.LayoutParams.MATCH_PARENT
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
//        params.verticalMargin = 0.2f
        params.gravity = Gravity.CENTER
        dialog!!.window!!.attributes = params as WindowManager.LayoutParams
//        dialog!!.window!!.setBackgroundDrawableResource(android.R.color.transparent)
        super.onStart()
    }

    private inner class BusinessAdapter(list: List<DistrictBean>): RecyclerView.Adapter<BusinessAdapter.MyViewHolder>() {
        private val mList: List<DistrictBean> = list

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val view = mInflater!!.inflate(R.layout.view_my_shop_business_circle, null)
            val viewHolder = MyViewHolder(view);
            return viewHolder
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            holder.tv_shop.text = mList.get(position).leaderShopName
            holder.tv_business.text = mList.get(position).bdName

            holder.cl_business.setOnClickListener(View.OnClickListener {
                itemClick.onItemClick(mList.get(position))
            })
        }

        override fun getItemCount(): Int {
            return mList.size
        }

        inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
            var iv_logo: ImageView
            var tv_shop: TextView
            var tv_business: TextView
            var cl_business: ConstraintLayout

            init {
                iv_logo = itemView.findViewById(R.id.shopAndDistrictLogo)
                tv_shop = itemView.findViewById(R.id.shopAndDistrictName)
                tv_business = itemView.findViewById(R.id.shopAndDistrictMessage)
                cl_business = itemView.findViewById(R.id.cl_business)
            }
        }
    }

    private lateinit var itemClick: ItemClick

    fun setItemClick(itemClick: ItemClick){
        this.itemClick = itemClick
    }

    interface ItemClick{
        fun onItemClick(bean: DistrictBean);
    }
}